﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesFastForward.Properties;
using System.Windows.Forms;
using System.IO;
using iTunesLib;
using System.Drawing;
using System.Configuration;

namespace iTunesFastForward
{
    public sealed class iTFFSettings : ApplicationSettingsBase
    {

        static iTFFSettings _instance;

        public static iTFFSettings Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new iTFFSettings();
                return _instance;
            }
        }

        public iTFFSettings() : base()
	    {
            Upgrade();
            PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(settings_PropertyChanged);
	    }

        void settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Save();
        }


        #region PC

        [UserScopedSettingAttribute()]
        public string PCexportTo
        {
            get { return (String)this["PCexportTo"]; }
            set { this["PCexportTo"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool PCreplaceItunesPath
        {
            get { return (bool)this["PCreplaceItunesPath"]; }
            set { this["PCreplaceItunesPath"] = value; }
        }

        [UserScopedSettingAttribute()]
        public string PCitunesPath
        {
            get { return (String)this["PCitunesPath"]; }
            set { this["PCitunesPath"] = value; }
        }

        [UserScopedSettingAttribute()]
        public string PCreplacementString
        {
            get { return (String)this["PCreplacementString"]; }
            set { this["PCreplacementString"] = value; }
        }

        [UserScopedSettingAttribute()]
        public string PCselectedPlaylists
        {
            get { return (String)this["PCselectedPlaylists"]; }
            set { this["PCselectedPlaylists"] = value; }
        }
        #endregion

        #region ShortCuts
        public struct ShortcutPreview
        {
            public int Value { get; set; }
            public Keys Modifiers { get; set; }
            public Keys Keys { get; set; }
            public string Name { get; set; }
            public string Representation { get; set; }
        }

        [UserScopedSettingAttribute()]
        public SerializableDictionary<string, ShortcutPreview> Shortcuts
        {
            get { return (SerializableDictionary<string, ShortcutPreview>)this["Shortcuts"]; }
            set { this["Shortcuts"] = value; }
        }

        public Shortcut GetShortCut(string shortcutName, Shortcut sc)
        {

            if (Shortcuts != null && Shortcuts.ContainsKey(shortcutName))
            {
                sc.Name = shortcutName;
                sc.Modifiers = Shortcuts[shortcutName].Modifiers;
                sc.Representation = Shortcuts[shortcutName].Representation;
                sc.Keys = Shortcuts[shortcutName].Keys;
                sc.Value = Shortcuts[shortcutName].Value;
                return sc;
            }
            else
                return null;
        }

        public void SetShortCut(Shortcut sc)
        {
            if (Shortcuts == null)
                Shortcuts = new SerializableDictionary<string, ShortcutPreview>();


            if (sc.Keys == Keys.None)
            {
                UnsetShortCut(sc);
                return;
            }

            Shortcuts[sc.Name] = new ShortcutPreview { Keys = sc.Keys, Modifiers = sc.Modifiers, Name = sc.Name, Representation = sc.Representation, Value = sc.Value };
            Save();
        }

        public void UnsetShortCut(Shortcut sc)
        {
            if (Shortcuts == null)
                Shortcuts = new SerializableDictionary<string, ShortcutPreview>();

            Shortcuts.Remove(sc.Name);
            Save();
        }
        #endregion

        #region Covers
        [UserScopedSettingAttribute()]
        [DefaultSettingValue("false")]
        public bool CoversStoreMinimizedImage
        {
            get { return (bool)this["CoversStoreMinimizedImage"]; }
            set { this["CoversStoreMinimizedImage"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool CoversApplyArtworkToEntireAlbum
        {
            get { return (bool)this["CoversApplyArtworkToEntireAlbum"]; }
            set { this["CoversApplyArtworkToEntireAlbum"] = value; }
        }
        #endregion

        #region lastfm
        [UserScopedSettingAttribute()]
        [DefaultSettingValue("false")]
        public bool LastFMEnable
        {
            get { return (bool)this["LastFMEnable"]; }
            set { this["LastFMEnable"] = value; }
        }

        [UserScopedSettingAttribute()]
        public string LastFMUsername
        {
            get { return (string)this["LastFMUsername"]; }
            set { this["LastFMUsername"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("0")]
        public int LastFMPasswordLength
        {
            get { return (int)this["LastFMPasswordLength"]; }
            set { this["LastFMPasswordLength"] = value; }
        }

        [UserScopedSettingAttribute()]
        public string LastFMPasswordHash
        {
            get { return (string)this["LastFMPasswordHash"]; }
            set { this["LastFMPasswordHash"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("0")]
        public int LastFMSongsSubmitted
        {
            get { return (int)this["LastFMSongsSubmitted"]; }
            set { this["LastFMSongsSubmitted"] = value; }
        }

        [UserScopedSettingAttribute()]
        public List<LastFM.TrackSubmission> LastFMSongsQueuedForSubmission
        {
            get { return (List<LastFM.TrackSubmission>)this["LastFMSongsQueuedForSubmission"]; }
            set { this["LastFMSongsQueuedForSubmission"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("0")]
        public int LastFMSubmissionAttempts
        {
            get { return (int)this["LastFMSubmissionAttempts"]; }
            set { this["LastFMSubmissionAttempts"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("0")]
        public int LastFMSuccessfullSubmissions
        {
            get { return (int)this["LastFMSuccessfullSubmissions"]; }
            set { this["LastFMSubmissionAttempts"] = value; }
        }
        #endregion

        #region Toaster

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool ToasterEnable
        {
            get { return (bool)this["ToasterEnable"]; }
            set { this["ToasterEnable"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("3")]
        public int ToasterShowToasterTimer
        {
            get { return (int)this["ToasterShowToasterTimer"]; }
            set { this["ToasterShowToasterTimer"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool ToasterHideOnClick
        {
            get { return (bool)this["ToasterHideOnClick"]; }
            set { this["ToasterHideOnClick"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool ToasterReappearOnMouseHOver
        {
            get { return (bool)this["ToasterReappearOnMouseHOver"]; }
            set { this["ToasterReappearOnMouseHOver"] = value; }
        }
        #endregion

        #region Lyrics
        public class PointLocation
        {
            public int X { get; set; }
            public int Y { get; set; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool LyricsAutoDownload
        {
            get { return (bool)this["LyricsAutoDownload"]; }
            set { this["LyricsAutoDownload"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool LyricsStoreIntoTags
        {
            get { return (bool)this["LyricsStoreIntoTags"]; }
            set { this["LyricsStoreIntoTags"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("false")]
        public bool LyricsStoreTextFiles
        {
            get { return (bool)this["LyricsStoreTextFiles"]; }
            set { this["LyricsStoreTextFiles"] = value; }
        }

        [UserScopedSettingAttribute()]
        [DefaultSettingValue("true")]
        public bool LyricsExcludePodcasts
        {
            get { return (bool)this["LyricsExcludePodcasts"]; }
            set { this["LyricsExcludePodcasts"] = value; }
        }

        [UserScopedSettingAttribute()]
        public string LyricsTextFilesPath
        {
            get { return (string)this["LyricsTextFilesPath"]; }
            set { this["LyricsTextFilesPath"] = value; }
        }

        public Point? LyricsFormPointLocation
        {
            get
            {
                if (LyricsFormLocation == null)
                    return null;
                return new Point(((PointLocation)LyricsFormLocation).X, ((PointLocation)LyricsFormLocation).Y); 
            }

            set
            {
                LyricsFormLocation = new PointLocation { X = value.Value.X, Y = value.Value.Y };
            }
        }

        [UserScopedSettingAttribute()]
        public PointLocation LyricsFormLocation
        {
            get { return (PointLocation)this["LyricsFormLocation"]; }
            set { this["LyricsFormLocation"] = value; }
        }

        #endregion
    }
}
